package com.example.phoneaddress.util;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

/**
 * Created by Administrator on 2021/3/30.
 */

public class DBUtils {
    public static String searchAddress(Context context, String phone) {
        String address="";
        //打开数据库
        //参数1-数据库文件的路径，参数2-游标工厂null，参数3-打开模式
        SQLiteDatabase db=SQLiteDatabase.openDatabase(context.getFilesDir()+"/address.db",null,SQLiteDatabase.OPEN_READONLY);

        //查
        //判断手机号码合法，正则表达式
        String regex="^1[3,4,5,6,7,8,9]\\d{9}";
        if (phone.matches(regex)){
            //合法手机号
            address=searchMobileAddress(db,phone);
        }else{
            //不合法的手机号
            switch (phone.length()){
                case 3:
                    address="紧急电话";
                    break;
                case 5:
                    address="运营商电话";
                    break;
                case 7:
                case 8:
                    address="本地座机";
                    break;
                case 11:
                    //根据区号，查找到所在城市
                    String area=phone.substring(0,3);
                    //Cursor cursor=db.rawQuery("select city from info where area=?",new String[]{area});
                    Cursor cursor=db.query("info",new String[]{"city"},"area=?",new String[]{area},null,null,null);
                    if (cursor.moveToNext()){
                        //区号是3位
                        address=cursor.getString(0);
                    }else {
                        //区号是4位
                        area=phone.substring(0,4);
                        Cursor cursor2=db.query("info",new String[]{"city"},"area=?",new String[]{area},null,null,null);
                        if (cursor2.moveToNext()){
                            address=cursor2.getString(0);
                        }else {
                            address="未知号码";

                        }

                    }
                    //address="长途电话";

                    break;
                default:
                    address="未知号码";

            }



        }
        return address;

    }
    private static String searchMobileAddress(SQLiteDatabase db, String phone) {
        phone=phone.substring(0,7);//截取号码的前7位
        Cursor cursor=db.query("info",new String[]{"cardtype"},"mobileprefix=?",new String[]{phone},null,null,null);
        String address="";
        if (cursor!=null&&cursor.getCount()>0){
            cursor.moveToNext();
            address=cursor.getString(0);
            //Toast.makeText(getApplicationContext(),address,Toast.LENGTH_LONG).show();
        }else {
            //Toast.makeText(getApplicationContext(),"未知号码",Toast.LENGTH_LONG).show();
        }
        db.close();
        cursor.close();
        /*
        Cursor cursor=db.rawQuery("select  * from info where mobileprefix=?",new String[]{phone});
        cursor.moveToNext();
        String address=cursor.getString(4);
        Toast.makeText(getApplicationContext(),address,Toast.LENGTH_LONG).show();
        */
        return  address;
    }
}
